//
//  MyMembershipCardViewController.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/16.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MyMembershipCardViewController.h"
#import "MyMembershipCardTableViewCell.h"
#import "MemberPrepaidPhoneViewController.h"
#import "TopUpHistoryViewController.h"
#import "PayPasswordViewController.h"

@interface MyMembershipCardViewController ()<UITableViewDelegate , UITableViewDataSource>

Strong UIView *headView;
Strong UITableView *tableView;
Strong NSMutableArray *dataArray; /**<*/
Strong UIImageView *titleImageView;
Strong UILabel *moneyLabel;  //余额
Strong UILabel *numberLabel; //卡号

@end

//网络
@implementation MyMembershipCardViewController(NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"/LaundryMall/Login_Registration/displayUserInformation.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"responseStatus"] isEqualToString:@"0"]) {
            NSDictionary *dic = responseObject[@"obj"];
            self.moneyLabel.text = [NSString stringWithFormat:@"余额:%@",dic[@"balance"]];
            self.numberLabel.text = [NSString stringWithFormat:@"NO:%@",dic[@"cardNumber"]];
            self.dataArray[0][0][@"content"] = dic[@"cardNumber"];
            self.dataArray[0][1][@"content"] = dic[@"grade"];
            self.dataArray[0][2][@"content"] = dic[@"createTime"];
            [self.tableView reloadData];
            
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
        [self hiddenProgressHUD];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self showError:error];
        [self hiddenProgressHUD];
    }];
}

@end

//ui
@implementation MyMembershipCardViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的会员卡";
    [self.view addSubview:self.tableView];
}

#pragma mark - init

- (UILabel *)numberLabel{
    if (!_numberLabel) {
        _numberLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 130 / WIDTH_5S_SCALE, SCREEN_WIDTH - 62 / WIDTH_5S_SCALE - 10 / WIDTH_5S_SCALE, 30 / WIDTH_5S_SCALE)];
        _numberLabel.font = DEF_FontSize_16;
        _numberLabel.textColor = getColor(@"ffffff");
        _numberLabel.textAlignment = NSTextAlignmentRight;
        _numberLabel.text = [NSString stringWithFormat:@"NO:%@",@"00000000"];
    }
    return _numberLabel;
}

- (UILabel *)moneyLabel{
    if (!_moneyLabel) {
        _moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 / WIDTH_5S_SCALE, 130 / WIDTH_5S_SCALE, 200 / WIDTH_5S_SCALE, 30 / WIDTH_5S_SCALE)];
        _moneyLabel.font = DEF_FontSize_16;
        _moneyLabel.textColor = getColor(@"ffffff");
        _moneyLabel.text = [NSString stringWithFormat:@"余额:%@",@"0.00"];
    }
    return _moneyLabel;
}

- (UIImageView *)titleImageView{
    if (!_titleImageView) {
        _titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(31 / WIDTH_5S_SCALE, 14 / WIDTH_5S_SCALE, SCREEN_WIDTH - 62 / WIDTH_5S_SCALE, 191 / WIDTH_5S_SCALE - 28 / WIDTH_5S_SCALE)];
        _titleImageView.contentMode = UIViewContentModeScaleAspectFill;
        _titleImageView.image = [UIImage imageNamed:@"VIP_backImage"];
        
        _titleImageView.layer.shadowColor = [UIColor blackColor].CGColor;
        _titleImageView.layer.shadowRadius= 4;
        _titleImageView.layer.shadowOpacity = .5;
        _titleImageView.layer.shadowOffset = CGSizeMake(0, 3);//偏移距离
        [_titleImageView sd_addSubviews:@[self.moneyLabel , self.numberLabel]];
    }
    return _titleImageView;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 191 / WIDTH_5S_SCALE)];
        _headView.backgroundColor = getColor(@"eeeeee");
        [_headView addSubview:self.titleImageView];
    }
    return _headView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [UIView new];
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
    }
    return _tableView;
}


#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.dataArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyMembershipCardTableViewCell *cell = [MyMembershipCardTableViewCell cellForTableView:tableView];
    cell.indexPath = indexPath;
    cell.model = self.dataArray[indexPath.section][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 5 / WIDTH_5S_SCALE;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5 / WIDTH_5S_SCALE)];
    view.backgroundColor = getColor(@"eeeeee");
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:{
                MemberPrepaidPhoneViewController *vc = [MemberPrepaidPhoneViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:
            {
                TopUpHistoryViewController *vc = [TopUpHistoryViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 2:
            {
                PayPasswordViewController *vc = [PayPasswordViewController new];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 3:
            {
                
            }
                break;
        }
    }
}



#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[@[@{@"image":@"user",@"title":@"卡号:",@"content":@"000000000"}.mutableCopy,
                        @{@"image":@"TheMembershipCard",@"title":@"等级:",@"content":@"VIP等级"}.mutableCopy,
                        @{@"image":@"stateTime",@"title":@"开始时间:",@"content":@"2017-02-22"}.mutableCopy,
                          ].mutableCopy,
                        @[@{@"image":@"top_up",@"title":@"充值"}.mutableCopy,
                          @{@"image":@"record",@"title":@"充值历史"}.mutableCopy,
                          @{@"image":@"payPassword",@"title":@"支付密码"}.mutableCopy,
//                          @{@"image":@"ConsumerList",@"title":@"消费清单"}.mutableCopy,
                          ].mutableCopy
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
